package io.sage.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;

import io.sage.annotation.Login;
import io.sage.annotation.LoginUser;
import io.sage.common.utils.R;
import io.sage.config.RedisUtil;
import io.sage.modules.employer.entity.Employer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 测试接口
 *
 */
@RestController
@RequestMapping("/api")
@Api(tags="测试接口")
public class ApiTestController {
	
	@Autowired
    private RedisUtil redisUtil;

    @Login
    @GetMapping("userInfo")
    @ApiOperation(value="获取用户信息", response=Employer.class)
    public R userInfo(@ApiIgnore @LoginUser Employer student){
        return R.ok().put("student", student);
    }

    @Login
    @GetMapping("userId")
    @ApiOperation("获取用户ID")
    public R userInfo(@ApiIgnore @RequestAttribute("userId") Integer userId){
    	System.out.println(userId);
        return R.ok().put("userId", userId);
    }
    
    @Login
    @PostMapping("userIdTest")
    @ApiOperation("获取用户ID111")
    public R userInfoTest(@RequestBody JSONObject jsonObject){
    	
    	Integer userId = jsonObject.getInteger("userId");
    
    	
        return R.ok().put("userId", userId);
    }

    @GetMapping("notToken")
    @ApiOperation("忽略Token验证测试")
    public R notToken(){
    	
        return R.ok().put("msg", "无需token也能访问。。。");
    }
    
    @GetMapping("redis")
    @ApiOperation("redis验证测试")
    public R redis(){
    	
    	String redis = redisUtil.get("redis");
    	if(redis!=null){
    		R.ok(redis);
    	}else {
    		redisUtil.set("redis", "我是redis缓存");
		}
        return R.ok().put("msg", redis);
    }

}
